
package com.myopen.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.myopen.kit.UUIDKit;

public class Attr extends Model<Attr>{

	private static final long serialVersionUID = 6785996853666596560L;
	public static final Attr dao=new Attr();
	
	public Page<Record> pagequery(Record record){
		StringBuffer sqlExceptSelect=new StringBuffer("from t_shop_attr t "
				+ " left join t_shop_category c on t.categoryId=c.id"
				+ " where t.removed='0' ");
		List<Object> list=new ArrayList<Object>();
		
		if(StrKit.notBlank(record.getStr("searchContent"))){
			sqlExceptSelect.append(" and c.name like ? ");
			list.add("%"+record.getStr("searchContent")+"%");
		}

		if(StrKit.notBlank(record.getStr("categoryId"))){
			sqlExceptSelect.append(" and c.id = ? ");
			list.add(record.getStr("categoryId"));
		}

		sqlExceptSelect.append(" order by t.createTime desc");
		return Db.paginate(record.getInt("pageNumber"),record.getInt("pageSize"),"select t.*,c.name categoryName ",sqlExceptSelect.toString(),list.toArray());		
    }
	

	public List<Record> getAttrValues(String attrId){
		List<Record> attrValues=Db.find("select * from t_shop_attr_value t where t.attrId=? order by px ",attrId);
		return attrValues;
	}

	public Record getRecordById(String id){

		Record bo=Db.findById("t_shop_attr", id);
		bo.set("attrValues", getAttrValues(id));

		return bo;

	}

	public Record getAttrValueRecordById(String id){

		Record bo=Db.findById("t_shop_attr_value", id);

		return bo;
	}

	public List<Record> getListByCategoryId(String categoryId){
		String sql="select * from t_shop_attr t where t.categoryId=? ";
		List<Record> list=Db.find(sql,categoryId);
		for (Record r : list) {
			r.set("attrValues", getAttrValues(r.getStr("id")));
		}

		return list;
	}

	public Record getRecordByCategoryId(String categoryId){
		String sql="select * from t_shop_attr t where t.categoryId=? ";
		return Db.findFirst(sql,categoryId);
	}

	public Record save(Record vo,List<Record> attrValues,Record currUser){
		if(StrKit.isBlank(vo.getStr("id"))){

			String id=UUIDKit.getUUID();

			vo.set("id", id);
			vo.set("removed", "0");
			vo.set("createTime", new Date());
			vo.set("updateTIme", new Date());
			Db.save("t_shop_attr", vo);

		}else{
			
			vo.set("updateTIme", new Date());
			Db.update("t_shop_attr",vo);

		}

		//不允许删除，只允许修改
		for(int i=0;i<attrValues.size();i++){
			Record attrValue=attrValues.get(i);
			if(StrKit.isBlank(attrValue.getStr("id"))){
				attrValue.set("id", UUIDKit.getUUID());
				attrValue.set("removed", "0");
				attrValue.set("createTime", new Date());
				attrValue.set("updateTime", new Date());
				attrValue.set("attrId", vo.getStr("id"));
				Db.save("t_shop_attr_value", attrValue);
			}else{
				Db.update("t_shop_attr_value", attrValue);
			}
			
		}


		return vo;

	}
    

}
